package com.zeratul.common.satoken.inter;

import com.zeratul.common.core.constant.MdcConstants;
import com.zeratul.common.satoken.utils.LoginHelper;
import com.zeratul.web.interceptor.base.BaseHandlerInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.MDC;

public class SaTokenLogInterceptor extends BaseHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取用户ID
        String userId = LoginHelper.getUserId();
        //获取用户类型
        String userType = LoginHelper.getUserType().name();
        //记录MDC
        MDC.put(MdcConstants.USER_ID, userId);
        MDC.put(MdcConstants.USER_TYPE, userType);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //回收MDC
        MDC.clear();
    }

    /**
     * 拦截器的拦截Path路径
     *
     * @return
     */
    @Override
    public String path() {
        return "/**";
    }
}
